package com.saa.role.service;


import com.saa.application.service.ApplicationInfoService;
import com.saa.application.vo.SaaApplicationInfoVo;
import com.saa.dept.po.SaaDept;
import com.saa.privilege.dao.SaaPrivilegeDao;
import com.saa.privilege.po.SaaPrivilege;
import com.saa.privilege.vo.SaaPrivilegeVo;
import com.saa.role.dao.SaaRoleDao;
import com.saa.role.dao.SaaRoleDeptDao;
import com.saa.role.dao.SaaRolePrivilegeDao;
import com.saa.role.po.SaaRole;
import com.saa.role.po.SaaRoleDept;
import com.saa.role.po.SaaRolePrivilege;
import com.saa.role.vo.SaaRoleDeptVo;
import com.saa.role.vo.SaaRoleVo;
import ins.framework.utils.Beans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SaaRoleService {
    public static final Logger logger = LoggerFactory.getLogger(SaaRoleService.class);
    @Autowired
    private SaaRoleDao saaRoleDao;
    @Autowired
    private SaaRoleDeptDao saaRoleDeptDao;
    @Autowired
    private SaaRolePrivilegeDao saaRolePrivilegeDao;
    @Autowired
    private SaaPrivilegeDao saaPrivilegeDao;
    @Autowired
    private ApplicationInfoService applicationInfoService;
    /***根据ID查询角色**/
    public SaaRoleVo getSaaRoleVo(String id){
        SaaRole saaRole = saaRoleDao.selectByPrimaryKey(id);
        SaaRoleVo saaRoleVo = new SaaRoleVo();
        Beans.copy().from(saaRole).to(saaRoleVo);
        return saaRoleVo;
    }

    /***根据应用代码和允许机构查询角色***/
    public List<SaaRoleVo> getSaaRoleVoListByApplicationCodeAndComCode(SaaRoleVo roleVo){
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        List<String> applicationIds = new ArrayList<String>();
/*        List<String> deptIds = new ArrayList<String>();
        List<SaaRoleDeptVo> saaRoleDeptVoList = roleVo.getSaaRoleDeptVoList();
        for(SaaRoleDeptVo saaRoleDeptVo:saaRoleDeptVoList){
            deptIds.add(saaRoleDeptVo.getDeptId());
        }*/
        List<SaaApplicationInfoVo> saaApplicationInfoVoList = roleVo.getSaaApplicationInfoVoList();
        for(SaaApplicationInfoVo saaApplicationInfoVo:saaApplicationInfoVoList){
            applicationIds.add(saaApplicationInfoVo.getApplicationCode());
        }
        map.put("applicationId",applicationIds);
        List<SaaRoleVo> saaRoleVoList = new ArrayList<SaaRoleVo>();
        List<SaaRole> saaRoles = saaRoleDao.selectRoleByApplicationIdAndDeptId(roleVo.getRoleCode(),roleVo.getName(),map);
        for(SaaRole saaRole :saaRoles){
            SaaRoleVo saaRoleVo = new SaaRoleVo();
            Beans.copy().from(saaRole).to(saaRoleVo);
            String applicationName = applicationInfoService.getSaaApplicationInfo(saaRole.getApplicationId());
            saaRoleVo.setApplicationName(applicationName);
            saaRoleVoList.add(saaRoleVo);
        }
        return saaRoleVoList;
    }

    /****查询角色下的机构***/
    public List<SaaRoleDept> getSaaRoleByDept(String roleId){
        List<SaaRoleDept> saaRoleDepts = saaRoleDeptDao.selectRoleDeptByRoleId(roleId);
        return saaRoleDepts;
    }

    /****根据角色查询权限和子权限***/
    public List<SaaPrivilegeVo> getPrivilegeList(String roleId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleId",roleId);
        List<SaaRolePrivilege> saaRolePrivileges = saaRolePrivilegeDao.selectRolePrivilegeByRoleId(map);
        List<String> privilegeIds = new ArrayList<String>();
        for(SaaRolePrivilege saaRolePrivilege :saaRolePrivileges){
            privilegeIds.add(saaRolePrivilege.getPrivilegeId());
        }
        if(privilegeIds.size()>0){
            List<SaaPrivilege> saaPrivileges = saaPrivilegeDao.selectBatchByPrimaryKeys(privilegeIds);
            List<SaaPrivilege> saaPrivilegeList = new ArrayList<SaaPrivilege>();
            for(SaaPrivilege saaPrivilege:saaPrivileges){
                if(saaPrivilege.getPrivilegeUpperId()==null){
                    saaPrivilegeList.add(saaPrivilege);
                }
            }
            return this.getSubSaaPrivilegeVoList(saaPrivilegeList,saaPrivileges);
        }
        return null;
    }

    public List<SaaPrivilegeVo> getSubSaaPrivilegeVoList(List<SaaPrivilege> upperSaaPrivilegeVos,
                                                         List<SaaPrivilege> saaPrivilegeVos){
        List<SaaPrivilegeVo> saaPrivilegeVoList = new ArrayList<SaaPrivilegeVo>();
        for(SaaPrivilege upp:upperSaaPrivilegeVos){
            SaaPrivilegeVo privilegeVo = new SaaPrivilegeVo();
            Beans.copy().from(upp).to(privilegeVo);
            List<SaaPrivilegeVo> subPrivilegeVoList = new ArrayList<SaaPrivilegeVo>();
            for(SaaPrivilege saaPrivilegeVo:saaPrivilegeVos){
                if(saaPrivilegeVo.getPrivilegeUpperId()!=null&&
                        upp.getId().equals(saaPrivilegeVo.getPrivilegeUpperId())){
                    SaaPrivilegeVo subPrivilegeVo = new SaaPrivilegeVo();
                    Beans.copy().from(saaPrivilegeVo).to(privilegeVo);
                    subPrivilegeVoList.add(privilegeVo);
                }
            }
            privilegeVo.setSubSaaPrivilegeVos(subPrivilegeVoList);
            saaPrivilegeVoList.add(privilegeVo);
        }
        return saaPrivilegeVoList;
    }

}
